
import { Component, Input } from '@angular/core';
import { LongRunningTaskInstanceStatusEnum, LrtmService } from '../lrtm.service';

/**
 * Task component. Used by LRTM component
 *
 * Author: Sorin Spiru
 * Date: 13.01.2018
 */
@Component({
    moduleId: module.id,
    templateUrl: 'task.component.html',
    selector: 'task',
    styleUrls: ['./task.component.scss'],
    providers: [LrtmService]
})
export class TaskComponent {
    @Input() expanded: boolean;

    @Input() index: number;

    @Input() task: any;

    constructor(private lrtmService: LrtmService) {
    }

    public taskFinished(task: any): boolean {
        return task.taskInstanceStatus == LongRunningTaskInstanceStatusEnum.Failed ||
            task.taskInstanceStatus == LongRunningTaskInstanceStatusEnum.Finished;
    }

    public isDownload(): boolean{
        return this.task.isDownload && !this.task.isUpload;
    }

    public hasStatus(status: string): boolean {
        let ret: boolean = false;
        switch(status) {
            case 'running':
                ret = this.task.taskInstanceStatus == LongRunningTaskInstanceStatusEnum.Running;
                break;
            case 'finished':
                ret = this.task.taskInstanceStatus == LongRunningTaskInstanceStatusEnum.Finished;
                break;
            case 'scheduled':
                ret = this.task.taskInstanceStatus == LongRunningTaskInstanceStatusEnum.Scheduled;
                break;
            case 'cancelled':
                ret = this.task.taskInstanceStatus == LongRunningTaskInstanceStatusEnum.Cancelled;
                break;
            case 'failed':
                ret = this.task.taskInstanceStatus == LongRunningTaskInstanceStatusEnum.Failed;
                break;
        }

        return ret;
    }

    // cancel task
    public cancelTask() {
        this.lrtmService.deleteTask(this.task.id).subscribe(_=>{
        });
    }

    // close task
    public closeTask() {
        this.lrtmService.deleteTask(this.task.id).subscribe(_=>{
        });
    }

    // download
    public downloadTask() {
        this.lrtmService.downloadResult(this.task.id);
    }

    get isGenerating(): boolean {
        return (this.hasStatus('running'));
    }

    get isComplete(): boolean {
        return (this.task.taskStatus.progress === 100);
    }

    get isFinished(): boolean {
        return this.hasStatus('finished');
    }

    get isFailed(): boolean {
        return (this.hasStatus('failed'));
    }
}